<template>

    <div>
        <el-dialog
                :title="title"
                :visible.sync="dialogVisible"
                width="30%"
        >
            <el-form :model="form" :rules="rules" ref="form" label-width="100px" class="demo-ruleForm">
                <el-form-item label="角色名" prop="role">
                    <el-input v-model="form.rname" ></el-input>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="dialogVisible = false">取 消</el-button>
                <el-button type="primary" @click="saveOrUpdate">确 定</el-button>
  </span>
        </el-dialog>

        <el-dialog
                title="权限分配"
                :visible.sync="visible2"
                width="30%"
        >
            <template>
                <el-tree
                        ref="treeRef"
                        :data="data2"
                        show-checkbox
                        default-expand-all
                        node-key="mid"
                        :default-checked-keys="defaultChecked"
                        :props="defaultProps">
                </el-tree>
            </template>
            <span slot="footer" class="dialog-footer">
    <el-button @click="visible2 = false">取 消</el-button>
    <el-button type="primary" @click="getCheckedKeys">确 定</el-button>
  </span>
        </el-dialog>

        <el-row>
            <el-col :span="6"><el-button type="success" class="addOne" @click="showWin()">新增</el-button></el-col>
            <el-col :span="12"><el-input v-model="roleName" placeholder="搜索角色"></el-input></el-col>
            <el-col :span="6"><el-button icon="el-icon-search" @click="load" circle></el-button></el-col>
        </el-row>
        <el-table
                :data="tableData"
                style="width: 100%">
            <el-table-column
                    prop="rid"
                    label="角色编号"
            >
            </el-table-column>
            <el-table-column
                    prop="rname"
                    label="角色名"
            >
            </el-table-column>
            <el-table-column
                    fixed="right"
                    label="操作"
            >
                <template slot-scope="scope">
                    <el-button @click="showWin(scope.row)" type="text" size="small">编辑</el-button>
                    <el-button type="danger" @click="del(scope.row)" icon="el-icon-delete" circle></el-button>
                    <el-button @click="cz(scope.row)" type="text" size="small">分配权限</el-button>
                </template>
            </el-table-column>
        </el-table>
        <el-pagination
                background
                @current-change="currentChange"
                layout="prev, pager, next"
                :page-size="pageSize"
                :current-page="currentPage"
                :total="total">
        </el-pagination>
    </div>
</template>

<script>

    export default {
        created() {
            this.loadData();
            //查询所有菜单
            this.$http.post("role/selectAllMenu",).then(res=>{
                console.info("查到的所有权限",res)
                this.data2=res.data;
            });
        },
        name: "role",
        data(){
            return{
                roleId:0,
                tableData:[],
                imageUrl: "",
                visible2: false,
                roleName:"",
                pageSize:10,
                currentPage:1,
                total:1,
                dialogVisible:false,
                title:"",
                form:{
                    rname:""
                },
                data2:[],
                defaultProps: {
                children: 'children',
                    label: 'mname'
                 },
                defaultChecked:[],
                rules: {
                    rname: [
                        { required: true, message: '请输入角色名', trigger: 'blur' },
                        { min: 2, max: 10, message: '长度在 2 到 10 个字符', trigger: 'blur' }
                    ]
                }
            }
        },
        methods:{
            loadData(){
                this.$http.post("role/selectAll",{rname:this.roleName,currentPage:this.currentPage,pageSize:this.pageSize}).then(res=>{
                    console.info(res)
                    this.tableData=res.data.list;
                    this.total=res.data.total;
                })
            },
            currentChange(currentPage){
                this.currentPage=currentPage;
                this.loadData()
            },
            load(){
                this.loadData()
            },
            showWin(row){
                this.dialogVisible=true;
                if (row){
                    this.form=Object.assign({},row);
                    this.title="修改角色信息"
                }else {
                    this.form={}
                    this.title="新增角色信息"
                }
            },
            saveOrUpdate() {
                this.$http.post("role/saveOrUpdate",this.form).then(res=>{
                    if (res.data.success){
                        this.dialogVisible=false
                        this.loadData()
                    }
                })
            },
            del(row){
                this.$http.post("role/delete",{rid:row.rid}).then(res=>{
                    if (res.data.success){
                        this.loadData()
                    }
                })
            },
            cz(row){
                this.visible2=true;
                this.roleId=row.rid;
                this.$http.post("role/selectQxId",{rid:row.rid}).then(res=>{
                    // console.info("查到的可以操作的权限权限",res)
                    // this.defaultChecked=res.data;
                    this.$refs.treeRef.setCheckedKeys(res.data)
                })
            },
            getCheckedKeys(){
                // 调用 getCheckedKeys 方法获取被选中叶子节点的 key 数组
                let checkedKeys1 = this.$refs.treeRef.getCheckedKeys();
                //调用getHalfCheckedKeys 方法获取半选中节点的key数组
                let halfCheckedKeys = this.$refs.treeRef.getHalfCheckedKeys();
                let allKeys = [...checkedKeys1, ...halfCheckedKeys];
                let jsAllkeys=JSON.stringify(allKeys)
                // console.info("合并后",allKeys)
                this.$http.post("role/insertPremit",{rid:this.roleId,mids:jsAllkeys}).then(res=>{
                    console.info("后台",res)
                    if (res.data.success){
                        this.visible2=false
                    }
                })
            }

        }
    }
</script>

<style scoped>
    .addOne{
        margin: 5px;
    }

</style>